package com.xjgc.common.utils;

import org.springframework.util.StringUtils;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;

/**
 * 日期时间相关工具
 */
public class DateTimeUtils {
    /*
     * String转换Timestamp
     */
    public static Timestamp stringTotimeStamp(String dateString) {
        if (dateString.length() < 10) {
            dateString = dateString.substring(0, 4) + "-" + dateString.substring(4, 6) + "-"
                    + dateString.substring(6, 8);
        }
        StringBuffer ds = new StringBuffer(dateString);
        if (dateString.length() > 10) {
            Timestamp ts = Timestamp.valueOf(dateString);
            return ts;
        } else {
            ds.append(" 00:00:00");
            Timestamp ts = Timestamp.valueOf(ds.toString());
            return ts;
        }
    }


    public static Date strToSqlDate_old(String date) throws ParseException {
        java.util.Date date1 = new SimpleDateFormat("yyyyMMdd").parse(date);
        return new Date(date1.getTime());
    }

    public static Date strToSqlDate(String date) throws ParseException {
        if ("--".equals(date) || StringUtils.isEmpty(date) || "0000-00-00".equals(date) || "00000000".equals(date)) {
            date = "1900-01-01";
        }
        java.util.Date date1 = null;
        if (date.contains("-")) {
            date1 = new SimpleDateFormat("yyyy-MM-dd").parse(date);
        } else {
            date1 = new SimpleDateFormat("yyyyMMdd").parse(date);
        }
        return new Date(date1.getTime());
    }

    public static Date strToDate(String date) throws ParseException {
        if (StringUtils.isEmpty(date)) {
            return null;
        }
        if ("--".equals(date) || "0000-00-00".equals(date)) {
            date = "1900-01-01";
        }
        java.util.Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(date);
        return new Date(date1.getTime());
    }

    public static Time strToSqlTime_old(String time) {
        String hour = time.substring(0, 2);
        String minute = time.substring(2, 4);
        String second = time.substring(4, 6);
        return Time.valueOf(LocalTime.of(Integer.parseInt(hour), Integer.parseInt(minute), Integer.parseInt(second)));
    }

    public static Time strToSqlTime(String time) {
        if (StringUtils.isEmpty(time)) {
            return null;
        }
        if (time.indexOf(":") == -1) {
            return strToSqlTime_old(time);
        }
        String[] split = time.split(":");
        return Time.valueOf(LocalTime.of(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2])));
    }

    /*
    根据日期获取时间字符串
    * */
    public static String timeToStr(java.util.Date date) {
        if (null == date) {
            return "000000";
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sf.format(date);
        strDate = strDate.substring(11, 19);
        return strDate.replace(":", "");
    }


    public static String dateToStr(java.util.Date date) {
        if (null == date) {
            return "1900-00-00";
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        String strDate = sf.format(date);
        return strDate;
    }

    public static String dateToStrOrNull(java.util.Date date) {
        if (null == date) {
            return null;
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        String strDate = sf.format(date);
        return strDate;
    }

    public static boolean isAfter(Date date1, Date date2) {
        return date1.toLocalDate().isAfter(date2.toLocalDate());
    }



}
